﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Net.Mail;

public partial class SendMail : System.Web.UI.Page
{
    MSSQLHelp.SqlDbHelper _sqlhelp = new MSSQLHelp.SqlDbHelper();
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Lb_SendStar_Click(object sender, EventArgs e)
    {
        DataTable dt = _sqlhelp.ExecuteDataTable("SMTP_GetList", CommandType.StoredProcedure, null);
        string _smtpadd = dt.Rows[0]["smtpmail"].ToString();//smtp服务器地址
        int _port = int.Parse(dt.Rows[0]["port"].ToString());//邮箱密码
        string username = dt.Rows[0]["username"].ToString();//邮箱名称
        string userpass = dt.Rows[0]["userpass"].ToString();//邮箱密码
        string mailaddress = this.Tb_mail.Text.Trim();//发送邮箱地址
        string mailtitle = this.Tb_title.Text.Trim();//发送邮件主题
        string mailconten = this.CKEditorControl1.Text;//发送邮件内容
        _SendMail(_smtpadd, username, userpass, mailaddress, mailtitle, mailconten);

    }
    /// <summary>
    /// 发送邮件的方法
    /// </summary>
    /// <param name="smtpadd">smtp服务器地址</param>
    /// <param name="smtpadd">smtp服务器端口</param>
    /// <param name="username">smtp邮箱名称</param>
    /// <param name="userpass">smpt邮箱密码</param>
    /// <param name="mailaddress">邮件发送地址</param>
    /// <param name="mailtitle">邮件标题</param>
    /// <param name="mailconten">邮件内容</param>
    private void _SendMail(string smtpadd,string username, string userpass,string mailaddress,string mailtitle,string mailconten)
    {
        SmtpClient _mysmtp = new SmtpClient(smtpadd);
        _mysmtp.Credentials = new System.Net.NetworkCredential(username, userpass);
        MailMessage _MsgMail = new MailMessage();
        _MsgMail.From = new MailAddress(username);
        _MsgMail.To.Add(mailaddress);
        _MsgMail.Subject = mailtitle;
        AlternateView alt = AlternateView.CreateAlternateViewFromString(mailconten, null, "text/html");
        _MsgMail.AlternateViews.Add(alt);
        _mysmtp.Send(_MsgMail);
    }
}